
/** TP2 **/

#include "font.h"
#include "fontdata.h"
#include <string>

#include <QtOpenGL>

using std::string;

Font::Font()
{
    Init();
}

void Font::MakeRasterFont(void)
{
    GLuint i, j;

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    fontOffset = glGenLists (128);

    for (i = 0,j = 'A'; i < 26; i++,j++) {
        glNewList(fontOffset + j, GL_COMPILE);
            glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, fontLetter[i]);
        glEndList();
    }

    glNewList(fontOffset + ' ', GL_COMPILE);
        glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, fontData);
    glEndList();
}

void Font::Init(void)
{
    MakeRasterFont();
}

void Font::Draw(int x, int y, string t)
{
    char * text = new char [t.size()+1];
    strcpy(text, t.c_str());

    GLfloat white[3] = { 1.0, 1.0, 1.0 };

    glPushAttrib(GL_LIST_BIT);

    glListBase(fontOffset);

    glPushMatrix();

    glLoadIdentity();

    glRasterPos2i(x, y);

    glColor3fv(white);

    glCallLists(
        strlen(text),
        GL_UNSIGNED_BYTE,
        (GLubyte *) text
    );

    glPopMatrix();

    glPopAttrib();
}

void Font::Update(void)
{
   Draw(100,100,"THE QUICK BROWN FOX JUMPS OVER A LAZY DOG");
}

